import {UserWaitApi} from "@/api/user_api";
import {Observer} from "@/utils/observer/Observer";

/**
 * 候诊屏 -通用共享

   数据交互流程：
   1、初始化、医生登录、退出 都会执行获取全量医生信息，并缓存到私有内存
   2、同时会直接执行查询全量患者数据
   刷新消息：会从私有缓存的医生信息中提取医生ID 作为入参直接查询全量患者数据
 */
class WaitPatientCommonObserverBiz extends Observer{
  constructor(self, deviceInfo = {}, item) {
    super();
    // 外部组件
    this.catcheInfo = self;
    // 设备信息
    this.deviceInfo = deviceInfo;
    // 当前组件信息
    this.currentInfo = item || {};
    // 私有内存
    this.allWorkerList = [];
  }
  /**
   * 消息监听入口
   * @param bizType
   * @param msgContent
   */
  async update(bizType, msgContent) {
    let self = this.catcheInfo;
    try {
      switch (bizType) {
        case "doctorLogin":
          await this.doctorLogin(msgContent);
          break;
        case "doctorLogout":
          await this.doctorLogout(msgContent);
          break;
        case "refreshMsg":
          await this.refreshMsg(msgContent);
          break;
      }
    } catch (e) {
      console.error("消息数据加载失败：" + msgContent)
      self.handleException("sdkBizServices", "安卓消息消费异常 -- " + bizType + " -- >", JSON.stringify(e.stack));
    }

  }


  /**
   * 初始化
   */
  async init() {
    // 获取局部用的医生ID
    await this.getAllWorkerIdList();
    // 获取患者数据
    await this.getAllWaitPatient();
  }

  /**
   * 刷新消息
   * @param str
   */
  async refreshMsg(str) {
    await this.getAllWaitPatient();
  }

  /**
   * 医生登录
   * @param obj
   */
  async doctorLogin(obj) {
    await this.init();
  }

  /**
   * 医生退出
   * @param obj
   */
  async doctorLogout(obj) {
    await this.init();
  }

  /**
   * 获取全部医生ID
   * @constructor
   */
  async getAllWorkerIdList() {
    let self = this.catcheInfo;
    try {
      const {data, code} = await UserWaitApi.getWaitList({
        terminalId: self.deviceInfo.screenId,
        terminalType: self.deviceInfo.screenType,
        businessType: self.deviceInfo.businessType,
        tenantId: self.deviceInfo.hospitalId,
        dataSource: self.apiType || "0",
      })
      if (code === 200) {
        // 设置缓存
        this.allWorkerList = data || [];
      }
    } catch (e) {
      self.handleException('sdkBizServices', '获取全量在线医生失败 getWaitList', JSON.stringify(e.stack));
    }
  }


  /**
   * 刷新全量候诊列表
   * @param str
   * @returns {Promise<void>}
   */
  async getAllWaitPatient(str) {
    let self = this.catcheInfo;
    try {
      if (this.allWorkerList.length === 0){
        this.setFullWaitPatientData(data || []);
        return ;
      }
      // 医生ID
      const workerIdList = this.allWorkerList.map((item) => item.workerId);
      const {data, code} = await UserWaitApi.getFullWaitingAll({
        workerIdList: workerIdList,
        // 开启全量后台会替换成1000000
        waitNum: self.templateInfo.waitNum,
        // 是否全量
        waitAllNum: true,
        // 是否截取
        waitPatientSlice: self.waitingScreenWaitingData.waitPatientSlice,
        terminalId: self.deviceInfo.screenId,
        terminalType: self.deviceInfo.screenType,
        businessType: self.deviceInfo.businessType,
        tenantId: self.deviceInfo.hospitalId,
        dataSource: self.apiType || "0",
        queryRule: self.waitingScreenWaitingData.queryRule
      });
      if (code === 200) {
        // 给组件发送数据
        this.setFullWaitPatientData(data || []);
      }
    } catch (e) {
      self.handleException("sdkBizServices", "getAllWaitPatient -> 刷新全量候诊列表", JSON.stringify(e.stack));
    }
  }


  /**
   * 刷新患者数据
   * @param data
   */
  setFullWaitPatientData(data) {
    let self = this.catcheInfo;
    try {
      const formRef = self.$refs["vFormRef"];
      if (formRef) {
        formRef.executeChildMethod(this.currentInfo?.id, "waitPatientListInfo", data);
      }
    } catch (e) {
      self.handleException("sdkBizServices", "内部组件异常 -- waitPatientListInfo -- >", JSON.stringify(e.stack));
    }
  }

}

export {WaitPatientCommonObserverBiz};
